#!/usr/bin/env escript
%% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ft=erlang ts=4 sw=4 et
%% -------------------------------------------------------------------
%%
%% replsrv: Hamler REPL Server
%%
%% -------------------------------------------------------------------
-mode(compile).

-define(MOD, '$REPL').

main([Pa, Pz, File]) ->
    io:setopts([{encoding, unicode}]),
    Outdir = filename:dirname(File),
    true = code:add_path(Outdir),
    true = code:add_path(Pa),
    [_ = code:add_path(P) || P <- string:tokens(Pz, ": ")],
    Opts = [from_core, {outdir, Outdir}],
    reploop(#{file => File, opts => Opts}).

reploop(State = #{file := File, opts := Opts}) ->
    case io:get_line("") of
        eof -> halt(0);
        ":" ++ Cmd ->
            reploop(exec(Cmd, State));
        _Expr ->
            try
                code:purge(?MOD),
                {ok, _} = compile:file(File, Opts),
                code:load_file(?MOD),
                print(?MOD:it())
            catch
                Error:Reason:Stk ->
                    io:format("Error: ~p, Reason: ~p~nStacktrace:~p~n", [Error, Reason, Stk])
            end,
            reploop(State)
    end.

exec("quit" ++ _, _State) ->
    halt(0);
exec("help" ++ _, State) ->
    io:format("Hamler REPL 0.1.1~n"),
    State;
exec(Cmd, State) ->
    io:format("Unknown cmd: ~s", [Cmd]),
    State.

%%eval([], State) -> State;
%%eval(SExpr, #{bindings := Bindings}) ->
%%    {ok, Ts, _} = erl_scan:string(SExpr),
%%    Ts1 = case lists:reverse(Ts) of
%%              [{dot, _}|_] -> Ts;
%%              TsR -> lists:reverse([{dot,erl_anno:new(1)}|TsR])
%%          end,
%%    {ok, Expr} = erl_parse:parse_exprs(Ts1),
%%    {value, Value, NewBindings} = erl_eval:exprs(Expr, Bindings),
%%    io:format("~p~n", [Value]),
%%    #{bindings => NewBindings}.

print(I) when is_integer(I) ->
    io:format("~w~n", [I]);
print(A) when is_atom(A) ->
    io:format("~s~n", [A]);
print(T) ->
    io:format("~p~n", [T]).

